package testParse;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.Difference;
import org.custommonkey.xmlunit.XMLAssert;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.xml.sax.SAXException;

import parsers.VertisParse;

import junit.framework.TestCase;

public class TestVertisParse extends TestCase {
	
	private static final char sep = File.separatorChar;
	
	private static final String safeTestOutputName = "SafeTestOutput.xml";
	private static final String detailExpectedName = "DetailExpected.xml";
	
	private static final String safeTestOutputPath = "." + sep + "Test_Output";
	private static final String detailExpectedPath = "." + sep + "Test_Output";;
	
	
	private final VertisParse sfParse = new VertisParse("fredmeyer", 608 , 19212);
	//private final VertisParse sfParse = new VertisParse("safeway", 1885 , 19390);
	//private final VertisParse sfParse = new VertisParse("qfc", 881 , 19352);
	
	private  File expectedFile = new File(safeTestOutputPath + sep + safeTestOutputName); 
	private File expectedDetailFile = new File(detailExpectedPath + sep + detailExpectedName);
	
	//private final String testInput = "P:\\Parse\\Parse\\Test_Input\\newInput.txt"; 
	//private final String testInput = "P:\\Parse\\Parse\\Test_Input\\safetestParseInput.html";
	//private final String testInput = "P:\\Parse\\Parse\\Test_Input\\qfc.txt";
	private final String testInput = "." + sep + "Test_input" + sep + "fred.txt";
	
	private Document expectedXML;
	private Document expectedDetailXML;
	protected void setUp() throws Exception {
		super.setUp();
		SAXBuilder build = new SAXBuilder();
		
		expectedXML = build.build(expectedFile);
		expectedDetailXML = build.build(expectedDetailFile);
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	public void testGetDetailURLsandCategories()
	{
		String fileName = sfParse.getDetailURLsAndCategories(testInput,
"breakout.jsp?referrer=products&imageName={ARG0}&isBreakoutImage={ARG1}&pageNumber={ARG6}&breakoutName={ARG2}&itemId={ARG3}&showAsPage=true",
			"1");	
		
		
		File file = new File(fileName);
		Document doc=null;
		SAXBuilder build = new SAXBuilder();
		
		try {
			doc = build.build(file);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
       // XMLAssert.assertXMLEqual("comparing test xml to control xml", expectedXML, doc);

		
		XMLOutputter docString = new XMLOutputter();
		
		
		Diff myDiff;
		try {
			myDiff = new Diff(docString.outputString(expectedXML), docString.outputString(doc));
			String a = docString.outputString(expectedXML);
			String b = docString.outputString(doc);
			XMLAssert.assertXMLEqual(docString.outputString(expectedXML), docString.outputString(doc));//myDiff,myDiff.identical());
			
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		//assertEquals(true, expectedXML.equals(doc));
		
		/*FileReader fReader;
		try {
			fReader = new FileReader(fileName);
			
			BufferedReader buffRead = new BufferedReader(fReader);
			String line =null; 
			while((line =buffRead.readLine())!=null)
			{}
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
/*	public void testDb()
	{
	 
		try 
		{ 
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
			Connection _pConnection = DriverManager.getConnection("jdbc:odbc:Price","", ""); 
			Statement statement = _pConnection.createStatement();
			ResultSet rs = statement.executeQuery("select Store.zipcode from store");
			
			System.out.println(rs.next());
			System.out.println(rs.getInt(1));
		} 
		catch(Exception e) 
		{ 
			Boolean _bSavedStatus = false; 
			String _sMessage = e.getMessage(); 
			System.out.println(_sMessage);
		} 
		System.out.println("done");
		
	}*/
	public void testGetProductDetails()
	{
		
		String detailFile = sfParse.getProductDetails("." + sep + "Test_Output" + sep + "TestOutput.xml");
		File file = new File(detailFile);
		Document doc=null;
		SAXBuilder build = new SAXBuilder();
		
		try {
			doc = build.build(file);
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
       // XMLAssert.assertXMLEqual("comparing test xml to control xml", expectedXML, doc);

		
		XMLOutputter docString = new XMLOutputter();
		
		
		Diff myDiff;
		try {
			myDiff = new Diff(docString.outputString(expectedDetailXML), docString.outputString(doc));
			String a = docString.outputString(expectedDetailXML);
			String b = docString.outputString(doc);
			XMLAssert.assertXMLEqual(docString.outputString(expectedDetailXML), docString.outputString(doc));//myDiff,myDiff.identical());
			
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//testDb();
	}
	

}
